<?php
/**
 * 后台端--生活自由抽奖管理
 * @version：1.0.0
 * Date: 2023/05/10 11:17
 */
namespace App\backend\controllers\lottery_draw;

use App\backend\controllers\AdminBaseController;
use App\backend\models\BigWheelPrizeModel;
use App\backend\models\LifeFreeModel;
use App\backend\services\LifeFreeServices;
use App\Exceptions\InvalidRequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\backend\services\lotteryServices;
use App\backend\services\OpenPrizeServices;

class LifeFreeController extends AdminBaseController
{
    protected $class_name = 'LifeFree';

    /**
     * 生活自由抽奖列表
     * @desc 活动列表--分页
     * 搜索 活动名称  活动状态
     * 列表 活动名称  创建时间 开奖方式  中奖次数/中奖人数  活动状态 下单引导  操作
     */
    public function getList()
    {
        //分页显示
        $data = request()->all();
        //列表
        $lists = LifeFreeServices::getActivityList($data);
        return successJson('ok', $lists);
    }

    /**
     * 生活自由抽奖记录列表
     * @desc 抽奖记录--分页
     * 抽奖记录
     * 搜索 会员信息 状态 开始时间-结束时间
     * 列表 会员信息  抽奖券数量  中奖状态（中奖、未中奖）  参与时间
     * uid  ticket_num 中奖状态
     */
    public function lotteryRecordList()
    {
        $data = request()->all();
        //参数验证
        $param_res = LifeFreeServices::checkBaseParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //列表
        $lists = LifeFreeServices::getPrizeRecordList($data);
        return successJson('ok', $lists);

    }

    /**
     * 抽奖记录----奖券列表
     * @return JsonResponse
     */
    public function getTicketList()
    {
        $data = request()->all();
        //参数验证
        $param_res = LifeFreeServices::checkBaseParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //列表
        $lists = LifeFreeServices::getRecordTicketList($data);
        return successJson('ok', $lists);

    }

    /**
     * 获取活动信息
     */
    public function getActivity()
    {
        $data = request()->all();
        //参数验证
        $param_res = LifeFreeServices::checkBaseParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //获取信息
        $info = LifeFreeServices::getActivityInfo($data);
        return successJson('ok', $info);
    }

    /**
     * 删除活动
     */
    public function delActivity()
    {
        $data = request()->all();
        //参数验证
        $param_res = LifeFreeServices::checkBaseParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //删除
        $res = LifeFreeServices::deleteActivityData($data);
        if ($res) {
            return successJson('删除成功');
        } else {
            return errorJson('删除失败');
        }
    }

    /**
     * 创建抽奖--基础设置
     * @return JsonResponse
     */
    public function modifyActivity()
    {
        $data = request()->all();
        $validator = Validator::make(
            $data,
            LifeFreeModel::modify_rules(),
            LifeFreeModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0]);
        }
        //参数验证
        $param_res = LifeFreeServices::checkBaseSetParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }
        //更新
        $list = LifeFreeServices::modifyActivity($data);
        if (!$list) {
            return errorJson('操作失败！');
        }
        return successJson('操作成功！',$list);
    }

    /**
     * 创建抽奖--奖品设置
     * @return JsonResponse
     */
    public function prizeSetting()
    {
        $data = request()->all();
        $validator = Validator::make(
            $data,
            LifeFreeModel::modify_prize_rules(),
            LifeFreeModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        //参数验证
        $param_res = LifeFreeServices::checkPrizeSetParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //更新
        $list = LifeFreeServices::modifyActivity($data);
        if (!$list) {
            return errorJson('操作失败！');
        }
        return successJson('操作成功！');
    }

    /**
     * 创建抽奖--开奖设置
     * @return JsonResponse
     */
    public function drawPrizeSetting()
    {
        $data = request()->all();
        $validator = Validator::make(
            $data,
            LifeFreeModel::modify_draw_prize_rules(),
            LifeFreeModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0]);
        }
        //参数验证
        $param_res = LifeFreeServices::checkDrawPrizeSetParam($data);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //更新
        $list = LifeFreeServices::modifyActivity($data);
        if (!$list) {
            return errorJson('操作失败！');
        }
        return successJson('操作成功！');
    }



    /**
     * 自由生活 开奖
     * @return int|void
     */
    public function goOpenDraw(){
        //自由生活去开奖
        $open_local = LifeFreeServices::goOpenDraw();
        echo "<pre>";
        print_r($open_local);
        die();
    }

}
